package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.agriculture.web.form.UVO;
import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;

@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {
	
	@Autowired
	HelloWorldService helloWorldService;
	private String message;
	
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
    	UserBean userBean = new UserBean();
    	model.addAttribute("userBean",userBean);
        return "helloWorld1";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST, params="login")
    public String initLogin(Model model,HttpSession session,@Valid @ModelAttribute("userBean")UserBean userBean,BindingResult results) {
    	if(results.hasErrors()){
    		log.info("内容验证出错！！");
    		return "helloWorld1";
    	}
    	List<UserBean> result = helloWorldService.searchUser(userBean);
    	if(result.size() != 0) {
    		UVO uvo=new UVO();
    		uvo.setUserId(userBean.getUserId());
    		uvo.setUserName(userBean.getUserName());
    		session.setAttribute("Uvo", uvo);
    		List<UserBean> result1 = helloWorldService.searchUser(null);
    		model.addAttribute("list",result1);
    		return "show";
    	} else { message="用户名或密码错误，请重新输入";
    	    model.addAttribute("message",message);
    		return "helloWorld1";
    	}
    }
    @RequestMapping(value = "/init1", method = RequestMethod.POST)
    public String initinsert(UserBean userBean,Model model) {
    	if(userBean.getPassWord().equals(userBean.getPassWord1()))
    	{   List<UserBean> result1 = helloWorldService.searchUser(userBean);
    	    if(result1.size()!=0)
    	   {message="您注册的ID已被别人使用！！请重新注册";
       	   model.addAttribute("message", message);
       	   return "register";
       	   }
    	    else
    		     if( helloWorldService.searchinsert(userBean)!=0) 
		          return "helloWorld1";
	       else  
		          return "helloWord";
    	}
    	else
    	  {	message="确认密码错误！！！！请重新输入您的密码并确认";
      	   model.addAttribute("message", message);
      	   return "register";
    	  }
    }
    @RequestMapping(value = "/init2", method = RequestMethod.POST)
    public String initupdate(UserBean userBean) {
    	
    	if( helloWorldService.searchupdate1(userBean)!=0) return "updatepage";
    	else  
    		return "helloWord";
    }
    
    @RequestMapping(value = "/init3", method = RequestMethod.POST,params="delete")
    public String initdelete(UserBean userBean,Model model) {
    	
    	if( helloWorldService.searchdelete(userBean)!=0) 
    		{List<UserBean> result = helloWorldService.searchUser(null);
    	      model.addAttribute("list", result);
    	     return "show";
    	}
    	else  
    		return "deletefalse";
    }
    @RequestMapping(value = "/init3", method = RequestMethod.POST,params="alter")
    public String initupdate1(Model model,@Valid @ModelAttribute("userBean") UserBean 
userBean, BindingResult results,HttpSession session) throws SQLException {
    	if(results.hasErrors()){
    		log.info("内容验证出错！！");
    		return "register1";
    	}
    	 if(userBean.getPassWord().equals(userBean.getPassWord1()))
    	 {log.info("修改用户信息");
    	 if(helloWorldService.searchupdate1(userBean)!=1)
    		 throw new SQLException("客户信息修改失败！");
    	 else
    	 { List<UserBean> result = helloWorldService.searchUser(null);
	      model.addAttribute("list", result);
	     return "show";
    	 }
    	 }
    	else{
    		model.addAttribute("message", "确认密码失败！！！！");
    		return "register1";
    	}
    }
    @RequestMapping(value = "/init4", method = RequestMethod.POST,params="register")
    public String initregister(Model model) {
    		return "register";
    }
    @RequestMapping(value = "/init4", method = RequestMethod.POST,params="sure")
    public String initupdate(UserBean userBean,Model model) {
    	    model.addAttribute("userBean",userBean);
    		return "register1";
    }
    @RequestMapping(value = "/init5", method = RequestMethod.GET)
    public String initdeleteconfrim(UserBean userBean,Model model) {
    	model.addAttribute("userBean", userBean);
    		return "deleteconfrim";
    }
    @RequestMapping(value = "/init6", method = RequestMethod.POST)
    public String initsearch(UserBean userBean,Model model) {
    	List<UserBean> result1 = helloWorldService.searchUser(null);
		model.addAttribute("list",result1);
		return "show";
    }
    @RequestMapping(value = "/init4", method = RequestMethod.POST,params="cancel")
    public String initsearch1(UserBean userBean,Model model) {
    	List<UserBean> result1 = helloWorldService.searchUser(null);
		model.addAttribute("list",result1);
		return "show";
    }
    @RequestMapping(value = "/init3", method = RequestMethod.POST,params="cancel")
    public String initsearch2(UserBean userBean,Model model) {
    	List<UserBean> result1 = helloWorldService.searchUser(null);
		model.addAttribute("list",result1);
		return "show";
    }
    @RequestMapping(value = "/init7", method = RequestMethod.GET)
    public String initupateconfrim(UserBean userBean,Model model) {
    	    model.addAttribute("userBean", userBean);
    		return "updateconfrim";
    }
}
